package com.jic.data.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.data.model.entity.DPv;
import com.jic.data.model.vo.request.DPvRequest;
import com.jic.data.service.DPvService;
import com.jic.data.utils.IPUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 上海奇陌科技有限公司
 * @date: 2020-11-28 17:26:20
 */
@RestController
@Slf4j
@RequestMapping("/pv")
@Api(tags = "PV控制类")
public class DPvController {

    private static ExecutorService executorService = Executors.newFixedThreadPool(20);

    @Autowired
    private DPvService service;

    @Resource
    private StringRedisTemplate redisClientTemplate;

    @ApiOperation(value = "新增PV")
    @PostMapping("/add")
    @ResponseBody
    public RestResult<Long> add(@RequestBody DPvRequest vo, HttpServletRequest req) throws Throwable {
        if (StringUtils.isBlank(vo.getChannelCode())) {
            return RestResult.error("9999", "渠道不能为空");
        }
        if (StringUtils.isBlank(vo.getPageViewUrl())) {
            return RestResult.error("9999", "用户浏览页面地址不能为空");
        }
        // 用户标识
        if (null == vo.getUserId()) {
            vo.setUserId(getUserId(vo.getToken()));
        }
        if (null == vo.getUserId() && StringUtils.isBlank(vo.getSessionId())) {
            return RestResult.error("9999", "用户标识不能为空");
        }
        //vo.setPageViewUrl(req.getRequestURL().toString());
        vo.setRefererUrl(req.getHeader("Referer"));
        vo.setIpAddress(IPUtils.getIP(req));
        vo.setCreateTimestamp(System.currentTimeMillis());
        vo.setCreateDateTimestamp(DateUtils.parseDate(DateFormatUtils.format(vo.getCreateTimestamp(), "yyyy-MM-dd"), new String[]{"yyyy-MM-dd"}).getTime());
        executorService.execute(() -> {
            DPv pv = new DPv();
            BeanUtils.copyProperties(vo, pv);
            service.save(pv);
        });
        return RestResult.success(null);
    }

    public Long getUserId(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        String userString = redisClientTemplate.opsForValue().get("H5_LOGIN_TOKEN_" + token);
        if (StringUtils.isEmpty(userString)) {
            return null;
        }
        JSONObject user = JSON.parseObject(userString);
        return user.getLong("id");
    }
}

	
